How to get a selected value from the group of elements with the same name?
How to get a selected value from the group of elements with the same name?
502
29-Aug-2023
Updated on 02-Sep-2023
Aryan Kumar
02-Sep-2023To get a selected value from a group of elements with the same name, you can use the
querySelector()method. ThequerySelector()method takes a selector as its argument and returns the first element that matches the selector.The following code shows how to get a selected value from a group of elements with the same name:
In this code, the
elementsvariable is a collection of all the elements that have thenameattribute set tomy_input. TheselectedValuevariable is the value of the first element in theelementscollection.If there is no selected element, the
querySelector()method will returnnull.Here is another way to get the selected value from a group of elements with the same name:
In this code, the
selectedValuevariable is the value of the element that has thenameattribute set tomy_input.